package com.xw.childhealth.controller;

import com.xw.childhealth.entity.PersonnelCode;
import com.xw.childhealth.entity.form.PersonnelCodeForm;
import com.xw.childhealth.service.PersonnelCodeService;
import com.xw.childhealth.util.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author: y
 * @CreateDate: 2019-11-12 09:05
 * @Version: 1.0
 */
@Controller
@RequestMapping("/personnelCode")
public class PersonnelCodeController {

    @Autowired
    private PersonnelCodeService personnelCodeService;

    static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * 获取人员代码信息
     *
     * @param size
     * @param page
     * @param start           开始时间
     * @param end             结束时间
     * @param name
     * @param personnelCoding
     * @param map
     * @return
     */
    @RequestMapping("/getPersonnelCode")
    public ModelAndView getPersonnelCode(@RequestParam(value = "size", defaultValue = "10") Integer size,
                                         @RequestParam(value = "page", defaultValue = "0") Integer page,
                                         @RequestParam(value = "start", required = false) String start,
                                         @RequestParam(value = "end", required = false) String end,
                                         @RequestParam(value = "name", required = false) String name,
                                         @RequestParam(value = "bianhao", required = false) String personnelCoding,
                                         Map<String, Object> map) {

        Specification<PersonnelCode> specification = new Specification<PersonnelCode>() {
            @Override
            public Predicate toPredicate(Root<PersonnelCode> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                //集合 用于封装查询条件
                List<Predicate> list = new ArrayList<Predicate>();
                if (!StringUtils.isEmpty(name)) {
                    Predicate p1 = criteriaBuilder.like(root.get("name").as(String.class), "%" + name.trim() + "%");
                    list.add(p1);
                }
                if (!StringUtils.isEmpty(personnelCoding)) {
                    Predicate p2 = criteriaBuilder.like(root.get("personnelCoding").as(String.class), "%" + personnelCoding.trim() + "%");
                    list.add(p2);
                }

                if (!StringUtils.isEmpty(start) && !StringUtils.isEmpty(end)) {
                    Predicate p3 = criteriaBuilder.between(root.get("birthDate").as(String.class), start, end);
                    list.add(p3);
                } else if (!StringUtils.isEmpty(start)) {
                    Predicate p4 = criteriaBuilder.between(root.get("birthDate").as(String.class), start, "2222-02-02");
                    list.add(p4);
                }


                return criteriaBuilder.and(list.toArray(new Predicate[0]));
            }
        };

        Sort sort = Sort.by(Sort.Order.desc("createTime"));
        PageRequest pageRequest = PageRequest.of(page < 0 ? 0 : page, size, sort);
        Page<PersonnelCode> allPersonnelCode = personnelCodeService.findAllPersonnelCode(specification, pageRequest);
        map.put("allPersonnelCode", allPersonnelCode);
        map.put("currentPage", page);
        map.put("size", size);
        return new ModelAndView("/pages/admin/personnel-code", map);
    }

    /**
     * 准备更新某个对象
     *
     * @param type
     * @param id
     * @param map
     * @return
     */
    @RequestMapping("/data-toUpd")
    public ModelAndView edit(@RequestParam(name = "type") String type, @RequestParam(value = "id", required = false) Integer id, Map<String, Object> map) {

        if (type.equals("toUpd")) {
            PersonnelCode result = personnelCodeService.findOneById(id);
            map.put("result", result);
            map.put("type", type);
        }
        return new ModelAndView("/pages/admin/personnel-code-add");
    }

    /**
     * 更新
     *
     * @param personnelCodeForm
     * @return
     */
    @RequestMapping("/data-upd")
    public ModelAndView upd(PersonnelCodeForm personnelCodeForm) {


        if (personnelCodeForm.getType().equals("add")) {
            PersonnelCode personnelCode = new PersonnelCode();
            BeanUtils.copyProperties(personnelCodeForm, personnelCode);
            personnelCode.setCreateTime(new Date());
            personnelCode.setUpdateTime(new Date());
            personnelCodeService.save(personnelCode);
        } else if (personnelCodeForm.getType().equals("upd")) {
            PersonnelCode result = personnelCodeService.findOneById(personnelCodeForm.getId());
            Assert.notNull(result, "result not be null");
            personnelCodeForm.setCreateTime(result.getCreateTime());
            personnelCodeForm.setUpdateTime(new Date());
            BeanUtils.copyProperties(personnelCodeForm, result);
            personnelCodeService.save(result);
        }

        return new ModelAndView("redirect:/personnelCode/getPersonnelCode");
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @RequestMapping("/data-del")
    public ModelAndView del(@RequestParam("id") Integer id) {
        PersonnelCode result = personnelCodeService.findOneById(id);
        if (result != null) {
            personnelCodeService.delOneById(id);
        }
        return new ModelAndView("redirect:/personnelCode/getPersonnelCode");
    }
}
